package com.ctg.polling.web.dataobject;

import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

public class PollResultsDO {

	private List<PollResultDO> results;
	
	public PollResultsDO () {
		createEmptyResultsList();
	}
	
	public PollResultsDO (List<PollResultDO> results) {
		if (results != null) {
			this.results = results;
		} else {
			createEmptyResultsList();
		}
	}
	
	private void createEmptyResultsList() {
		this.results = new LinkedList<PollResultDO>();
	}
	
	public List<PollResultDO> getResultsSortedList() {
		return results;
	}
	
	public void addEntry(PollResultDO result) {
		results.add(result);
	}
	
	public void populatePercentFields () {
		float totalCount = 0;
		
		for (PollResultDO result : results) {
			totalCount += result.getCounter();
		}
		
		for (PollResultDO result : results) {
			int counter = result.getCounter();
			result.setPercent((counter * 100) / totalCount);
		}
		
		Collections.sort(results);
	}
}
